const THOUSAND = /\d{1,3}(?=(\d{3})+$)/g
export default {
  name: "homePage",
  data() {
    return {
		totalnum:'',
      eChart: {},
      tChart: {},
      aChart: {},
	  destruction: false,						//组件是否已经销毁
      totaluser: 0, // 用户注册总量
      totalcompany: 0, // 企业注册总量

      totalrecharge: 0, // 企业充值总金额
      companyrefundamount: 0, // 企业退款总额
      totalcompanymoney: 0, // 企业还可退总额
      deductionsamount: 0, // IOS平台扣费总额
	  discountSum: 0,	//奖励招聘积分发放总额
	  discountUseSum: 0,//奖励招聘积分使用总额

      totalScore: 0, // 用户积分总数
      totalout: 0, // 积分已提现
      totalintegral: 0, // 积分未提现
      personaltax: 0, // 积分已交个税金额
      activityReward: 0, // 活动奖励积分
	  awardIntegral:0,
      noPayTotal: 0, // 企业未支付订单总金额
      payTotal: 0, // 企业已支付订单总金额
      totalplatformpunish: 0, // 平台处罚积分
      totaltakeoutpunish: 0, // 抵扣处罚积分
	  awardIntegralCount: 0, //奖励积分
		
      educationType: [],
      educationNum: [],

      talentsNum: [],
      postNum: [],
      postType: [],

      totalAgeData: [],
      totalAgeType: [],

      manData: [],
      womanData: [],
      unfilledData: [],

      stageuser: 0, // 新增会员
      stagecompany: 0, // 新增企业
      stageintegral: 0, // 新增积分
      stagerecharge: 0, // 充值金额
      stageout: 0, // 已提现积分
      stageemployee: 0, // 录用人数
      stageperiodworkemployee: 0, // 入保障期人数
      stageperiodleaveemployee: 0, // 保障期内离职人数
      stageoutperiodemployee: 0, // 过保障期转正人数
      options: ["天", "周", "月", "季", "年"],
      type: "4", // 1->小时 2->天 3->周 4->月 5->季 6->年
      time: "月"
    };
  },
  mounted() {
	 this.gettatol();
    this.getTotalData();
    this.getStatisticsData();
    this.getDate();
  },
  destroyed(){
  	this.destruction = true;
  },
  methods: {
	  
	  gettatol(){
	  		 this.$ceshiGetAxios("/v1/common/platformAll").then(res => {
	  			 this.totalnum=res.data.data
	  		 })
	  },
    // 获取汇总统计数据
    getTotalData() {
      this.$axios.post("/app/v1/platform/summary/statistics").then(res => {
        if (res.data.code === 1) {
          let result = res.data.data;
		  console.log(res);
          ["totaluser", "totalcompany", "totalrecharge", "companyrefundamount", "awardIntegral","totalcompanymoney", "deductionsamount","discountSum","discountUseSum", "totalScore", "totalout", "totalintegral", "personaltax", "activityReward", "noPayTotal", "payTotal", "totalplatformpunish", "totaltakeoutpunish", "awardIntegralCount" ].forEach(el => {
            this[el] = result[el] || 0.00;
			if(el == 'totalScore'){
				localStorage.setItem('totalScore',result[el]);
			}
          });

          let educationArr = res.data.data.totalqualification;
          educationArr.forEach(item => {
            this.educationType.push(item.qualification);
            this.educationNum.push(item.number);
          });

          let talents1Arr = res.data.data.talent1;
          let talents2Arr = res.data.data.talent2;
          talents1Arr.forEach(item => {
              this.talentsNum.push(item.number);
            }),
            talents2Arr.forEach(item => {
              this.postNum.push(item.number);
              this.postType.push(item.name);
            });

          let totalageArr = res.data.data.totalage;
          totalageArr.forEach(item => {
            if (item.name == '60-') {
              item.name = '60岁以上'
            }
            this.totalAgeType.push(item.name);
          });

          this.totalAgeData = totalageArr.map(item => {
            if (item.name == '60-') {
              item.name = '60岁以上'
            }
            return {
              value: item.number,
              name: item.name
            };
          });

          let totalsexArr = res.data.data.totalsex;
          totalsexArr.forEach(item => {
            if (item.sex === "1") {
              // 1：男
              this.manData.push(item.number);
            } else if (item.sex === "2") {
              // 2：女
              this.womanData.push(item.number);
            } else if (item.sex === "3") {
              this.unfilledData.push(item.number);
            }
          });

          this.drawEchart();
          this.drawTchart();
          this.drawAchart();
          this.drawGchart();
        }
      });
    },
    // 获取运维统计数据
    getStatisticsData() {
      this.$axios.post("/app/v1/platform/summary/operation", {
        type: this.type,
		assessType:1,
		  assessCode:1
      }).then(res => {
        if (res.data.message === "success.") {
          let result = res.data.data;
          ["stageuser", "stagecompany", "stageintegral", "stagerecharge", "stageout", "stageemployee", "stageperiodworkemployee", "stageperiodleaveemployee", "stageoutperiodemployee"].forEach(el => {
            this[el] = result[el] || 0;
          });
        }
      });
    },
    // 获取当前日期时间
    getDate() {
      var currentDate = new Date();
      var year = currentDate.getFullYear(); // 年
      var month = currentDate.getMonth() + 1; // 月
      var date = currentDate.getDate(); // 日
      var day = currentDate.getDay(); // 一周的第几天
      if (day === 0) {
        day = 7;
      }
      var milliseconds = currentDate.getTime(); // 毫秒数
      var startdate;
      var enddate;
      var tempMonth;
      var tempDate;

      tempMonth = month < 10 ? "0" + month : month;
      tempDate = date < 10 ? "0" + date : date;
      enddate = year + "-" + tempMonth + "-" + tempDate;

      if (this.type === "1") {
        startdate = enddate;
      } else if (this.type === "2") {
        startdate = enddate;
      } else if (this.type === "3") {
        var startDate = new Date(milliseconds - (day - 1) * 86400000);
        let tempMonth =
          startDate.getMonth() + 1 < 10 ?
          "0" + (startDate.getMonth() + 1) :
          startDate.getMonth() + 1;
        let tempDate =
          startDate.getDate() + 1 < 10 ?
          "0" + startDate.getDate() :
          startDate.getDate();
        startdate = startDate.getFullYear() + "-" + tempMonth + "-" + tempDate;
      } else if (this.type === "4") {
        startdate = year + "-" + tempMonth + "-" + "01";
      } else if (this.type === "5") {
        if (month < 4) {
          startdate = year + "-" + "01" + "-" + "01";
        } else if (month >= 4 && month < 7) {
          startdate = year + "-" + "04" + "-" + "01";
        } else if (month >= 7 && month < 10) {
          startdate = year + "-" + "07" + "-" + "01";
        } else {
          startdate = year + "-" + "10" + "-" + "01";
        }
      } else if (this.type === "6") {
        startdate = year + "-" + "01" + "-" + "01";
      }
      var timeObj = {
        startdate: startdate,
        enddate: enddate
      };
      return timeObj;
    },
    getEduYAxisUnit(arg){
      const max = Math.max(...arg);
      return max > 10000 ? '万' : '个'
    },
    drawEchart() {
	  if(this.destruction){return false;}
      this.eChart = this.$echarts.init(this.$refs["education-chart"]);
      const unit = this.getEduYAxisUnit(this.educationNum)
      this.eChart.setOption({
        title: {
          text: "用户学历统计"
        },
        color: ["#0064e4"],
        tooltip: {},
        grid:{
          left: 50,
        },  
        barCategoryGap: "20",
        xAxis: {
          data: this.educationType,
          axisLabel: {
            rotate: 45
          }
        },
        yAxis: {
          name: "单位: " + unit,
          nameTextStyle: {},
          axisLabel: {
            formatter: function (value, index) {
              return  unit === '万' ? (value / 10000 + '').replace(THOUSAND,'$&,'): (value + '').replace(THOUSAND,'$&,')
            }
          }
        },
        series: [{
          name: "学历",
          type: "bar",
          barWidth: "15",
          data: this.educationNum
        }]
      });
    },
    drawTchart() {
	  if(this.destruction){return false;}
      this.tChart = this.$echarts.init(this.$refs["talents-chart"]);
      const unit = this.getEduYAxisUnit(this.talentsNum.concat(this.postNum));
      this.tChart.setOption({
        title: {
          text: "人才与招聘岗位统计"
        },
        grid:{
          left: 50,
        },  
        color: ["#fdbc34", "#00b7e9"],
        tooltip: {},
        legend: {
          data: ["人才", "招聘岗位"],
          top: "28",
          right: "56"
        },
        xAxis: {
          data: this.postType,
          axisLabel: {
            rotate: 45
          }
        },
        yAxis: {
          name: "单位: " + unit,
          nameTextStyle: {},
          axisLabel: {
            formatter: function (value, index) {
              return  unit === '万' ? (value / 10000 + '').replace(THOUSAND,'$&,'): (value + '').replace(THOUSAND,'$&,')
            }
          }
        },
        series: [{
            name: "人才",
            type: "bar",
            barWidth: "14",
            data: this.talentsNum
          },
          {
            name: "招聘岗位",
            type: "bar",
            barWidth: "14",
            data: this.postNum
          }
        ]
      });
    },
    drawAchart() {
	  if(this.destruction){return false;}
      var that = this;
      this.aChart = this.$echarts.init(this.$refs["age-chart"]);
      this.aChart.setOption({
        title: {
          text: "年龄段统计",
          padding: 20
        },
        color: ["#fdbc34", "#ff5f66", "#9051d6", "#00cb91", "#79b2ff"],
        tooltip: {
          formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        legend: {
          orient: "vertical",
          top: "20%",
          right: "4%",
          data: this.totalAgeType,
          formatter: function (name) {
            let data = that.totalAgeData;
            let sum = 0;
            for (var i = 0; i < data.length; i++) {
              sum = data[i].value + sum;
            }
            for (var j = 0; j < data.length; j++) {
              if (data[j].name == name && name != '60岁以上') {
                return " " + name + "岁 ";
              } else if (data[j].name == name && name == '60岁以上') {
                return " " + name;
              }
            }
          }
        },
        series: [{
          name: "年龄段",
          type: "pie",
          radius: ["50%", "30%"],
          avoidLabelOverlap: false,
          label: {
            normal: {
              show: false,
              position: "center"
            }
          },
          center: [80, 100],
          data: this.totalAgeData
        }]
      });
    },
    drawGchart() {
	  if(this.destruction){return false;}
      var womanPercent = parseInt(
        (this.womanData[0] / (this.manData[0] + this.womanData[0] + this.unfilledData[0])) * 100
      );
      // var womanPercent = parseInt(
      //   (50 / 100) * 100
      // );

      var manPercent = 100 - womanPercent;
      this.gChart = this.$echarts.init(this.$refs["gender-chart"]);
      this.gChart.setOption({
        title: {
          text: "男女比例统计",
          padding: 20
        },
        color: ["#ff5f66", "#00b7e9"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow"
          }
        },
        grid: {
          left: "3%",
          right: "20%",
          bottom: "3%",
          containLabel: true
        },
        xAxis: {
          show: false,
          type: 'value',
          max: 100
        },
        yAxis: {
          type: "category"
        },
        series: [{
            name: "女",
            type: "bar",
            itemStyle: {
              normal: {
                label: {
                  show: true,
                  position: "right",
                  formatter: " 女生" + womanPercent + "%"
                }
              }
            },
            // data: this.womanData
            data: [womanPercent]
          },
          {
            name: "男",
            type: "bar",
            itemStyle: {
              normal: {
                label: {
                  show: true,
                  position: "right",
                  formatter: " 男生" + manPercent + "%"
                }
              }
            },
            // data: this.manData
            data: [manPercent]
          }
        ]
      });
    },
    selected(item, index) {
      this.time = item;
      this.type = index + 2 + "";
      this.getStatisticsData();
      this.getDate();
    },
    linkto(name) {
      var timeObj = this.getDate();
      this.$router.push({
        name,
        params: {
          /* startdate: timeObj.startdate,
          enddate: timeObj.enddate */
        }
      });
    },
    // linkto(name, istime) {
    //   var timeObj = this.getDate();
    //   let startdate = istime? timeObj.startdate: "";
    //   let enddate = istime? timeObj.enddate: "";
    //   this.$router.push({
    //     name,
    //     params: {
    //       startdate,
    //       enddate
    //     }
    //   });
    // }
  }
};